package pers.dawnyang.common.util.sql;

import java.lang.reflect.Field;

import org.apache.commons.lang3.StringUtils;

/**
 *
 * @author dawn 2020年7月15日 上午11:33:55
 *
 */

public class SqlTools {

    public static <T> T montageSql(T obj) {
        if (null != obj) {
            Class<? extends Object> class1 = obj.getClass();
            Field[] fields = class1.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                if (field.isAnnotationPresent(Like.class)) {
                    try {
                        Object object = field.get(obj);
                        if (null != object && StringUtils.isNoneBlank(object.toString())) {
                            String str = "%" + object.toString() + "%";
                            field.set(obj, str);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else if (field.isAnnotationPresent(RLike.class)) {
                    try {
                        Object object = field.get(obj);
                        if (null != object && StringUtils.isNoneBlank(object.toString())) {
                            String str = "%" + object.toString();
                            field.set(obj, str);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else if (field.isAnnotationPresent(LLike.class)) {
                    try {
                        Object object = field.get(obj);
                        if (null != object && StringUtils.isNoneBlank(object.toString())) {
                            String str = object.toString() + "%";
                            field.set(obj, str);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } /*else if (field.isAnnotationPresent(IN.class)) {
                    try {
                        Object object = field.get(obj);
                        if (null != object && StringUtils.isNoneBlank(object.toString())) {
                            String newObject = "";
                            if (object instanceof Iterable) {
                                newObject += "(";
                                for (Object o : (Iterable)object) {
                                    newObject += o.toString();
                                    newObject += ",";
                                }
                                if (newObject.length() > 1) {
                                    newObject.substring(0, newObject.length() - 1);
                                }
                                newObject += ")";
                            }
                            field.set(obj, newObject);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                  }*/
            }
        }
        return obj;
    }

    public static void main(String[] args) {
        ParamTest param = new ParamTest();
        String[] strs = {"a", "b", "c"};
        param.setStrs(strs);
        montageSql(param);
        System.out.println(param.getStrs());
    }

}
